package com.duqc.tag.service.impl.strategy;

import cn.hutool.core.text.StrBuilder;
import cn.hutool.json.JSONUtil;
import com.duqc.tag.entity.ClassificationRule;
import com.duqc.tag.entity.classification.LabelRule;
import com.duqc.tag.entity.enumeration.TagTypeEnum;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;
import java.util.StringJoiner;

/**
 * @ClassName TagDataLabelRule
 * @Description TagDataLabelRule
 * @Author duqc
 * @Date 2023/2/9
 **/
@Slf4j
@Component
@TagStrategy
public class TagDataLabelRule implements TagDataStrategy {

    /**
     * 返回 type
     *
     * @return String
     */
    @Override
    public String getType() {
        return String.valueOf(TagTypeEnum.LABEL_RULE.getTagType());
    }

    /**
     * 获取标签数据
     *
     * @param objects  规则信息
     * @param relation 规则关系
     * @return List
     */
    @Override
    public List<String> calculationTag(List<ClassificationRule> objects, String relation) {
        List<String> sql = new ArrayList<>();
        objects.forEach(info -> {
            List<LabelRule> labelRules = JSONUtil.toList(info.getRules(), LabelRule.class);
            //sql 拼接
            List<String> sqlList = buildSql(labelRules);
            //todo or用union and用join
            unionSql(sqlList, sql);
        });
        String labelSql = "";
        if ("and".equals(relation)) {
            labelSql = buildJoinLabelSql(sql);
        }
        System.out.println(labelSql);
        return null;
    }

    /**
     * 构建 最终SQL
     *
     * @param sql SQL list
     * @return String
     */
    private String buildJoinLabelSql(List<String> sql) {
        StrBuilder strBuilder = new StrBuilder();
        strBuilder.append("SELECT a1.entity_id FROM");
        if (sql.size() > 1) {
            for (int i = 0; i < sql.size(); i++) {
                strBuilder.append(" (").append(sql.get(i)).append(") a").append(i + 1);
                if (i == 0) {
                    strBuilder.append(" JOIN ");
                } else if (i == sql.size() - 1) {
                    strBuilder.append(" USING(entity_id)");
                } else {
                    strBuilder.append(" USING(entity_id) JOIN ");
                }
            }
            return strBuilder.toString();
        }
        return sql.get(0);
    }

    /**
     * union sql拼接
     *
     * @param sqlList sql
     * @param sql     返回sql
     */
    private void unionSql(List<String> sqlList, List<String> sql) {
        StringJoiner stringJoiner = new StringJoiner(" UNION ");
        sqlList.forEach(stringJoiner::add);
        sql.add(stringJoiner.toString());
    }

    /**
     * 拼接SQL
     *
     * @param labelRules 规则信息
     * @return LIST
     */
    private List<String> buildSql(List<LabelRule> labelRules) {
        List<String> labelSql = new ArrayList<>();
        labelRules.forEach(info -> {
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append("SELECT entity_id FROM tag_data WHERE tag_id = \"");
            stringBuilder.append(info.getKeyData())
                    .append("\" AND tag_name = \"")
                    .append(info.getKeyCn())
                    .append("\"");
            labelSql.add(stringBuilder.toString());
        });
        return labelSql;
    }

}
